#ifndef K_TIMER_QUEUE_H
#define K_TIMER_QUEUE_H
#pragma once

#include "K_Windows.h"
#include "KNoCopy.h"

class KTimerQueue : KNoCopy {
public:
    KTimerQueue();
    ~KTimerQueue();
public:
    HANDLE handle() const   { return m_queue; }
    bool isAlive() const    { return handle() != NULL; }
public:
    bool stopAll();
private:
    HANDLE m_queue;
};

class KTimer : KNoCopy {
public:
    explicit KTimer(KTimerQueue *queue = NULL);
    virtual ~KTimer() = 0;
public:
    HANDLE handle() const   { return m_timer; }
    bool isAlive() const    { return handle() != NULL; }
public:
    bool reset(DWORD dueTime, DWORD period = 0);
    bool restart(DWORD dueTime, DWORD period = 0);
    bool start(DWORD dueTime, DWORD period = 0);
    bool stop();
protected:
    virtual void tick();
private:
    static VOID CALLBACK TimerProc(PVOID lpTimer, BOOLEAN);
private:
    HANDLE m_timer;
    KTimerQueue *m_queue;
};

inline bool KTimer::restart(DWORD dueTime, DWORD period)
{
    return stop() ? start(dueTime, period) : false;
}

#endif // K_TIMER_QUEUE_H